{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Zipline Coding Exercises - Solution\n",
    "\n",
    "In this notebook you will create your own pipeline and create custom factors and filters for it. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Install Packages"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Collecting zipline===1.3.0 (from -r requirements.txt (line 1))\n",
      "  Downloading https://files.pythonhosted.org/packages/be/59/8c5802a7897c1095fdc409fb557f04df8f75c37174e80d2ba58c8d8a6488/zipline-1.3.0.tar.gz (2.5MB)\n",
      "\u001b[K    100% |████████████████████████████████| 2.5MB 189kB/s eta 0:00:01    58% |██████████████████▊             | 1.4MB 8.7MB/s eta 0:00:01\n",
      "\u001b[?25hCollecting graphviz==0.9 (from -r requirements.txt (line 2))\n",
      "  Downloading https://files.pythonhosted.org/packages/47/87/313cd4ea4f75472826acb74c57f94fc83e04ba93e4ccf35656f6b7f502e2/graphviz-0.9-py2.py3-none-any.whl\n",
      "Requirement already satisfied: pip>=7.1.0 in /opt/conda/lib/python3.6/site-packages (from zipline===1.3.0->-r requirements.txt (line 1))\n",
      "Requirement already satisfied: setuptools>18.0 in /opt/conda/lib/python3.6/site-packages (from zipline===1.3.0->-r requirements.txt (line 1))\n",
      "Collecting Logbook>=0.12.5 (from zipline===1.3.0->-r requirements.txt (line 1))\n",
      "  Downloading https://files.pythonhosted.org/packages/74/fc/3e7557ed1ef1bd4e3ee189fc670416abfc7192b550e8d3c1d858a63f41ab/Logbook-1.4.1.tar.gz (84kB)\n",
      "\u001b[K    100% |████████████████████████████████| 92kB 3.6MB/s eta 0:00:01\n",
      "\u001b[?25hRequirement already satisfied: pytz>=2016.4 in /opt/conda/lib/python3.6/site-packages (from zipline===1.3.0->-r requirements.txt (line 1))\n",
      "Requirement already satisfied: numpy>=1.11.1 in /opt/conda/lib/python3.6/site-packages (from zipline===1.3.0->-r requirements.txt (line 1))\n",
      "Collecting requests-file>=1.4.1 (from zipline===1.3.0->-r requirements.txt (line 1))\n",
      "  Downloading https://files.pythonhosted.org/packages/23/9c/6e63c23c39e53d3df41c77a3d05a49a42c4e1383a6d2a5e3233161b89dbf/requests_file-1.4.3-py2.py3-none-any.whl\n",
      "Requirement already satisfied: scipy>=0.17.1 in /opt/conda/lib/python3.6/site-packages (from zipline===1.3.0->-r requirements.txt (line 1))\n",
      "Requirement already satisfied: pandas<=0.22,>=0.18.1 in /opt/conda/lib/python3.6/site-packages (from zipline===1.3.0->-r requirements.txt (line 1))\n",
      "Collecting pandas-datareader>=0.2.1 (from zipline===1.3.0->-r requirements.txt (line 1))\n",
      "  Downloading https://files.pythonhosted.org/packages/cc/5c/ea5b6dcfd0f55c5fb1e37fb45335ec01cceca199b8a79339137f5ed269e0/pandas_datareader-0.7.0-py2.py3-none-any.whl (111kB)\n",
      "\u001b[K    100% |████████████████████████████████| 112kB 3.2MB/s eta 0:00:01\n",
      "\u001b[?25hRequirement already satisfied: patsy>=0.4.0 in /opt/conda/lib/python3.6/site-packages (from zipline===1.3.0->-r requirements.txt (line 1))\n",
      "Requirement already satisfied: statsmodels>=0.6.1 in /opt/conda/lib/python3.6/site-packages (from zipline===1.3.0->-r requirements.txt (line 1))\n",
      "Requirement already satisfied: python-dateutil>=2.4.2 in /opt/conda/lib/python3.6/site-packages (from zipline===1.3.0->-r requirements.txt (line 1))\n",
      "Requirement already satisfied: six>=1.10.0 in /opt/conda/lib/python3.6/site-packages (from zipline===1.3.0->-r requirements.txt (line 1))\n",
      "Requirement already satisfied: requests>=2.9.1 in /opt/conda/lib/python3.6/site-packages (from zipline===1.3.0->-r requirements.txt (line 1))\n",
      "Requirement already satisfied: Cython>=0.25.2 in /opt/conda/lib/python3.6/site-packages (from zipline===1.3.0->-r requirements.txt (line 1))\n",
      "Collecting cyordereddict>=0.2.2 (from zipline===1.3.0->-r requirements.txt (line 1))\n",
      "  Downloading https://files.pythonhosted.org/packages/d1/1a/364cbfd927be1b743c7f0a985a7f1f7e8a51469619f9fefe4ee9240ba210/cyordereddict-1.0.0.tar.gz (138kB)\n",
      "\u001b[K    100% |████████████████████████████████| 143kB 2.6MB/s eta 0:00:01\n",
      "\u001b[?25hCollecting bottleneck>=1.0.0 (from zipline===1.3.0->-r requirements.txt (line 1))\n",
      "  Downloading https://files.pythonhosted.org/packages/05/ae/cedf5323f398ab4e4ff92d6c431a3e1c6a186f9b41ab3e8258dff786a290/Bottleneck-1.2.1.tar.gz (105kB)\n",
      "\u001b[K    100% |████████████████████████████████| 112kB 3.4MB/s eta 0:00:01\n",
      "\u001b[?25hCollecting contextlib2>=0.4.0 (from zipline===1.3.0->-r requirements.txt (line 1))\n",
      "  Downloading https://files.pythonhosted.org/packages/a2/71/8273a7eeed0aff6a854237ab5453bc9aa67deb49df4832801c21f0ff3782/contextlib2-0.5.5-py2.py3-none-any.whl\n",
      "Requirement already satisfied: decorator>=4.0.0 in /opt/conda/lib/python3.6/site-packages (from zipline===1.3.0->-r requirements.txt (line 1))\n",
      "Requirement already satisfied: networkx<2.0,>=1.9.1 in /opt/conda/lib/python3.6/site-packages (from zipline===1.3.0->-r requirements.txt (line 1))\n",
      "Requirement already satisfied: numexpr>=2.6.1 in /opt/conda/lib/python3.6/site-packages (from zipline===1.3.0->-r requirements.txt (line 1))\n",
      "Collecting bcolz<1,>=0.12.1 (from zipline===1.3.0->-r requirements.txt (line 1))\n",
      "  Downloading https://files.pythonhosted.org/packages/6c/8b/1ffa01f872cac36173c5eb95b58c01040d8d25f1b242c48577f4104cd3ab/bcolz-0.12.1.tar.gz (622kB)\n",
      "\u001b[K    100% |████████████████████████████████| 624kB 713kB/s eta 0:00:01\n",
      "\u001b[?25hRequirement already satisfied: click>=4.0.0 in /opt/conda/lib/python3.6/site-packages (from zipline===1.3.0->-r requirements.txt (line 1))\n",
      "Requirement already satisfied: toolz>=0.8.2 in /opt/conda/lib/python3.6/site-packages (from zipline===1.3.0->-r requirements.txt (line 1))\n",
      "Collecting multipledispatch>=0.4.8 (from zipline===1.3.0->-r requirements.txt (line 1))\n",
      "  Downloading https://files.pythonhosted.org/packages/89/79/429ecef45fd5e4504f7474d4c3c3c4668c267be3370e4c2fd33e61506833/multipledispatch-0.6.0-py3-none-any.whl\n",
      "Requirement already satisfied: MarkupSafe>=0.23 in /opt/conda/lib/python3.6/site-packages (from zipline===1.3.0->-r requirements.txt (line 1))\n",
      "Requirement already satisfied: Mako>=1.0.1 in /opt/conda/lib/python3.6/site-packages/Mako-1.0.7-py3.6.egg (from zipline===1.3.0->-r requirements.txt (line 1))\n",
      "Requirement already satisfied: sqlalchemy>=1.0.8 in /opt/conda/lib/python3.6/site-packages (from zipline===1.3.0->-r requirements.txt (line 1))\n",
      "Collecting alembic>=0.7.7 (from zipline===1.3.0->-r requirements.txt (line 1))\n",
      "  Downloading https://files.pythonhosted.org/packages/1a/37/8df0e37d730f096f5a41514823eaec3c5e169510a5c5ddc3dcbab5446ee8/alembic-1.0.1.tar.gz (1.0MB)\n",
      "\u001b[K    100% |████████████████████████████████| 1.0MB 449kB/s eta 0:00:01    48% |███████████████▍                | 491kB 17.1MB/s eta 0:00:01\n",
      "\u001b[?25hCollecting sortedcontainers>=1.4.4 (from zipline===1.3.0->-r requirements.txt (line 1))\n",
      "  Downloading https://files.pythonhosted.org/packages/be/e3/a065de5fdd5849450a8a16a52a96c8db5f498f245e7eda06cc6725d04b80/sortedcontainers-2.0.5-py2.py3-none-any.whl\n",
      "Collecting intervaltree>=2.1.0 (from zipline===1.3.0->-r requirements.txt (line 1))\n",
      "  Downloading https://files.pythonhosted.org/packages/ca/c1/450d109b70fa58ca9d77972b02f69222412f9175ccf99fdeaf167be9583c/intervaltree-2.1.0.tar.gz\n",
      "Collecting lru-dict>=1.1.4 (from zipline===1.3.0->-r requirements.txt (line 1))\n",
      "  Downloading https://files.pythonhosted.org/packages/00/a5/32ed6e10246cd341ca8cc205acea5d208e4053f48a4dced2b1b31d45ba3f/lru-dict-1.1.6.tar.gz\n",
      "Collecting empyrical>=0.5.0 (from zipline===1.3.0->-r requirements.txt (line 1))\n",
      "  Downloading https://files.pythonhosted.org/packages/7b/55/a01b05162b764830dbbac868462f44cd847a5b6523a01ca9f955721819da/empyrical-0.5.0.tar.gz (49kB)\n",
      "\u001b[K    100% |████████████████████████████████| 51kB 4.0MB/s eta 0:00:01\n",
      "\u001b[?25hCollecting tables>=3.3.0 (from zipline===1.3.0->-r requirements.txt (line 1))\n",
      "  Downloading https://files.pythonhosted.org/packages/d7/1b/21f4c7f296b718575c17ef25e61c05742a283c45077b4c8d5a190b3e0b59/tables-3.4.4-cp36-cp36m-manylinux1_x86_64.whl (3.8MB)\n",
      "\u001b[K    100% |████████████████████████████████| 3.8MB 119kB/s eta 0:00:01    34% |███████████▏                    | 1.3MB 30.2MB/s eta 0:00:01\n",
      "\u001b[?25hCollecting trading-calendars>=1.0.1 (from zipline===1.3.0->-r requirements.txt (line 1))\n",
      "  Downloading https://files.pythonhosted.org/packages/0c/5c/2bcf3a0e0ba8485c7d756176ff2b2b56c2802d072902fb5341a6231c2a9b/trading_calendars-1.4.2.tar.gz (86kB)\n",
      "\u001b[K    100% |████████████████████████████████| 92kB 4.3MB/s eta 0:00:01\n",
      "\u001b[?25hCollecting wrapt (from pandas-datareader>=0.2.1->zipline===1.3.0->-r requirements.txt (line 1))\n",
      "  Downloading https://files.pythonhosted.org/packages/a0/47/66897906448185fcb77fc3c2b1bc20ed0ecca81a0f2f88eda3fc5a34fc3d/wrapt-1.10.11.tar.gz\n",
      "Requirement already satisfied: lxml in /opt/conda/lib/python3.6/site-packages (from pandas-datareader>=0.2.1->zipline===1.3.0->-r requirements.txt (line 1))\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Requirement already satisfied: chardet<3.1.0,>=3.0.2 in /opt/conda/lib/python3.6/site-packages (from requests>=2.9.1->zipline===1.3.0->-r requirements.txt (line 1))\n",
      "Requirement already satisfied: idna<2.7,>=2.5 in /opt/conda/lib/python3.6/site-packages (from requests>=2.9.1->zipline===1.3.0->-r requirements.txt (line 1))\n",
      "Requirement already satisfied: urllib3<1.23,>=1.21.1 in /opt/conda/lib/python3.6/site-packages (from requests>=2.9.1->zipline===1.3.0->-r requirements.txt (line 1))\n",
      "Requirement already satisfied: certifi>=2017.4.17 in /opt/conda/lib/python3.6/site-packages (from requests>=2.9.1->zipline===1.3.0->-r requirements.txt (line 1))\n",
      "Collecting python-editor>=0.3 (from alembic>=0.7.7->zipline===1.3.0->-r requirements.txt (line 1))\n",
      "  Downloading https://files.pythonhosted.org/packages/65/1e/adf6e000ea5dc909aa420352d6ba37f16434c8a3c2fa030445411a1ed545/python-editor-1.0.3.tar.gz\n",
      "Building wheels for collected packages: zipline, Logbook, cyordereddict, bottleneck, bcolz, alembic, intervaltree, lru-dict, empyrical, trading-calendars, wrapt, python-editor\n",
      "  Running setup.py bdist_wheel for zipline ... \u001b[?25ldone\n",
      "\u001b[?25h  Stored in directory: /root/.cache/pip/wheels/a4/d6/67/f303ab028b004bf8e00c05b5b04fba83d8ec238b6547becdb7\n",
      "  Running setup.py bdist_wheel for Logbook ... \u001b[?25ldone\n",
      "\u001b[?25h  Stored in directory: /root/.cache/pip/wheels/06/13/e9/88e9e8184d89671ffc754dc80f5eb01dabd72071bdb802c5d1\n",
      "  Running setup.py bdist_wheel for cyordereddict ... \u001b[?25ldone\n",
      "\u001b[?25h  Stored in directory: /root/.cache/pip/wheels/0b/9d/8b/5bf3e22c1edd59b50f11bb19dec9dfcfe5a479fc7ace02b61f\n",
      "  Running setup.py bdist_wheel for bottleneck ... \u001b[?25ldone\n",
      "\u001b[?25h  Stored in directory: /root/.cache/pip/wheels/f2/bf/ec/e0f39aa27001525ad455139ee57ec7d0776fe074dfd78c97e4\n",
      "  Running setup.py bdist_wheel for bcolz ... \u001b[?25ldone\n",
      "\u001b[?25h  Stored in directory: /root/.cache/pip/wheels/c5/cc/1b/2cf1f88959af5d7f4d449b7fc6c9452d0ecbd86fd61a9ee376\n",
      "  Running setup.py bdist_wheel for alembic ... \u001b[?25ldone\n",
      "\u001b[?25h  Stored in directory: /root/.cache/pip/wheels/67/59/2e/bbf7e5d1ac878f9735223846512f71782bd7889e7820ff3740\n",
      "  Running setup.py bdist_wheel for intervaltree ... \u001b[?25ldone\n",
      "\u001b[?25h  Stored in directory: /root/.cache/pip/wheels/6b/cf/b0/f7ef2d0f504d26f3e9e70c2369e5725591ccfaf67d528fcbc5\n",
      "  Running setup.py bdist_wheel for lru-dict ... \u001b[?25ldone\n",
      "\u001b[?25h  Stored in directory: /root/.cache/pip/wheels/b7/ef/06/fbdd555907a7d438fb33e4c8675f771ff1cf41917284c51ebf\n",
      "  Running setup.py bdist_wheel for empyrical ... \u001b[?25ldone\n",
      "\u001b[?25h  Stored in directory: /root/.cache/pip/wheels/83/14/73/34fb27552601518d28bd0813d75124be76d94ab29152c69112\n",
      "  Running setup.py bdist_wheel for trading-calendars ... \u001b[?25ldone\n",
      "\u001b[?25h  Stored in directory: /root/.cache/pip/wheels/29/b1/59/6c32f717ffbf9622c9098d66576bfcda058c9412ebac0214a5\n",
      "  Running setup.py bdist_wheel for wrapt ... \u001b[?25ldone\n",
      "\u001b[?25h  Stored in directory: /root/.cache/pip/wheels/48/5d/04/22361a593e70d23b1f7746d932802efe1f0e523376a74f321e\n",
      "  Running setup.py bdist_wheel for python-editor ... \u001b[?25ldone\n",
      "\u001b[?25h  Stored in directory: /root/.cache/pip/wheels/36/e0/98/ba386b125a00ea9dd52e2c16aa2ec0adbbd639b84bfe2e001d\n",
      "Successfully built zipline Logbook cyordereddict bottleneck bcolz alembic intervaltree lru-dict empyrical trading-calendars wrapt python-editor\n",
      "Installing collected packages: Logbook, requests-file, wrapt, pandas-datareader, cyordereddict, bottleneck, contextlib2, bcolz, multipledispatch, python-editor, alembic, sortedcontainers, intervaltree, lru-dict, empyrical, tables, trading-calendars, zipline, graphviz\n",
      "Successfully installed Logbook-1.4.1 alembic-1.0.1 bcolz-0.12.1 bottleneck-1.2.1 contextlib2-0.5.5 cyordereddict-1.0.0 empyrical-0.5.0 graphviz-0.9 intervaltree-2.1.0 lru-dict-1.1.6 multipledispatch-0.6.0 pandas-datareader-0.7.0 python-editor-1.0.3 requests-file-1.4.3 sortedcontainers-2.0.5 tables-3.4.4 trading-calendars-1.4.2 wrapt-1.10.11 zipline-1.3.0\n",
      "\u001b[33mYou are using pip version 9.0.1, however version 18.1 is available.\n",
      "You should consider upgrading via the 'pip install --upgrade pip' command.\u001b[0m\n"
     ]
    }
   ],
   "source": [
    "import sys\n",
    "!{sys.executable} -m pip install -r requirements.txt"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Load Data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "from zipline.data import bundles\n",
    "from zipline.data.bundles.csvdir import csvdir_equities\n",
    "\n",
    "\n",
    "# Specify the bundle name\n",
    "bundle_name = 'm4-quiz-eod-quotemedia'\n",
    "\n",
    "# Create an ingest function \n",
    "ingest_func = csvdir_equities(['daily'], bundle_name)\n",
    "\n",
    "# Register the data bundle and its ingest function\n",
    "bundles.register(bundle_name, ingest_func);\n",
    "\n",
    "# Set environment variable 'ZIPLINE_ROOT' to the path where the most recent data is located\n",
    "os.environ['ZIPLINE_ROOT'] = os.path.join(os.getcwd(), '..', '..', 'data', 'module_4_quizzes_eod')\n",
    "\n",
    "# Load the data bundle\n",
    "bundle_data = bundles.load(bundle_name)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# TODO: Build an Empty Pipeline with a Screen\n",
    "\n",
    "In the code below create an empty with a screen that filters the pipeline output for stocks with a 60-day average dollar volume greater than \\$50,000."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# import resources\n",
    "from zipline.pipeline import Pipeline\n",
    "from zipline.pipeline.factors import AverageDollarVolume\n",
    "\n",
    "# Create a screen for our Pipeline\n",
    "universe = AverageDollarVolume(window_length = 60) > 50000\n",
    "\n",
    "# Create an empty Pipeline with the given screen\n",
    "pipeline = Pipeline(screen = universe)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# TODO: Create a Pipeline Engine\n",
    "\n",
    "In the code below, create a data loader that loads our data bundle using Zipline's `USEquityPricingLoader`. Then create a function to be passed to the `get_loader` parameter in the pipeline engine. Using the NYSE trading calendar create a pipeline engine using Zipline's `SimplePipelineEngine`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "# import resources\n",
    "from zipline.utils.calendars import get_calendar\n",
    "from zipline.pipeline.data import USEquityPricing\n",
    "from zipline.pipeline.engine import SimplePipelineEngine\n",
    "from zipline.pipeline.loaders import USEquityPricingLoader\n",
    "\n",
    "# Set the dataloader\n",
    "pricing_loader = USEquityPricingLoader(bundle_data.equity_daily_bar_reader, bundle_data.adjustment_reader)\n",
    "\n",
    "# Define the function for the get_loader parameter\n",
    "def choose_loader(column):\n",
    "    if column not in USEquityPricing.columns:\n",
    "        raise Exception('Column not in USEquityPricing')\n",
    "    return pricing_loader\n",
    "\n",
    "# Set the trading calendar\n",
    "trading_calendar = get_calendar('NYSE')\n",
    "\n",
    "# Create a Pipeline engine\n",
    "engine = SimplePipelineEngine(get_loader = choose_loader,\n",
    "                              calendar = trading_calendar.all_sessions,\n",
    "                              asset_finder = bundle_data.asset_finder)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# TODO: Create Factors and Filters For The Pipeline\n",
    "\n",
    "### Factors\n",
    "\n",
    "In the code below, create two factors:\n",
    "\n",
    "1. **Custom Factor**: Create a custom factor, `percent_difference`,  by combining a 30-day average closing price factor and a 60-average closing price factor. The `percent_difference` factor will calculate the difference between the 30-day and 60-average closing price factors, and will then normalize this difference by the 60-average closing price factor.\n",
    "\n",
    "\n",
    "2. **Daily Returns Factor**: Create a factor that returns the daily percent change in closing price. To do this, use Zipline's built-in [DailyReturns](https://www.zipline.io/appendix.html#zipline.pipeline.factors.DailyReturns) factor.\n",
    "\n",
    "Both factors should use the closing prices in the `USEquityPricing`  dataset as inputs. \n",
    "\n",
    "\n",
    "\n",
    "\n",
    "### Filters\n",
    "\n",
    "In the code below, you will create a **Custom Filter**, `tradable_asset`, that returns `True` for the top 20 securities of average dollar volume in a 30 day-window that have a latest closing price above \\$30. To do this, you will create two filters first and them combine them. For the first filter, you can use Zipline's built-in factor `AverageDollarVolume()` and its method `.top()` to select the top 20 assets of average dollar volume in a 30 day-window. To get the latest close price of the `USEquityPricing` dataset you can use `USEquityPricing.close.latest`. Finally you can combine these two filters by using the logical operator `&`. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "# import resources\n",
    "import pandas as pd\n",
    "from zipline.pipeline.factors import SimpleMovingAverage\n",
    "from zipline.pipeline.factors import DailyReturns\n",
    "\n",
    "# Create the 30-day average closing price factor\n",
    "mean_close_30 = SimpleMovingAverage(inputs=[USEquityPricing.close], window_length=30)\n",
    "\n",
    "# Create the 60-day average closing price factor\n",
    "mean_close_60 = SimpleMovingAverage(inputs=[USEquityPricing.close], window_length=60)\n",
    "\n",
    "# Create the custom factor\n",
    "percent_difference = (mean_close_30 - mean_close_60) / mean_close_60\n",
    "\n",
    "# Create the daily returns factor\n",
    "daily_ret = DailyReturns(inputs=[USEquityPricing.close])\n",
    "\n",
    "# Create a filter for the top 20 securities of average dollar volume in a 30 day-window\n",
    "top_20 = AverageDollarVolume(window_length=30).top(20)\n",
    "\n",
    "# Create a filter for the latest closing price above $30\n",
    "above_30 = USEquityPricing.close.latest > 30\n",
    "\n",
    "# Create a custum filter\n",
    "tradable_asset = top_20 & above_30"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# TODO: Add Factors and Filters To The Pipeline\n",
    "\n",
    "In the code below, add the factors and filters you created above to the pipeline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Add the custom factor to the pipeline\n",
    "pipeline.add(percent_difference, 'Percent Difference')\n",
    "\n",
    "# Add the daily returns factor to the pipeline\n",
    "pipeline.add(daily_ret, 'Daily Return')\n",
    "\n",
    "# Add the custom filter to the pipeline\n",
    "pipeline.add(tradable_asset, 'Tradable')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# TODO: Run The Pipeline\n",
    "\n",
    "In the code below, run the pipeline for the dates given"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>Daily Return</th>\n",
       "      <th>Percent Difference</th>\n",
       "      <th>Tradable</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"5\" valign=\"top\">2014-01-06 00:00:00+00:00</th>\n",
       "      <th>Equity(0 [A])</th>\n",
       "      <td>0.006951</td>\n",
       "      <td>0.025939</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Equity(1 [AAL])</th>\n",
       "      <td>0.046523</td>\n",
       "      <td>0.055887</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Equity(2 [AAP])</th>\n",
       "      <td>0.028611</td>\n",
       "      <td>0.051009</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Equity(3 [AAPL])</th>\n",
       "      <td>-0.021972</td>\n",
       "      <td>0.035906</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Equity(4 [ABBV])</th>\n",
       "      <td>0.006147</td>\n",
       "      <td>0.033018</td>\n",
       "      <td>False</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                            Daily Return  Percent Difference  \\\n",
       "2014-01-06 00:00:00+00:00 Equity(0 [A])         0.006951            0.025939   \n",
       "                          Equity(1 [AAL])       0.046523            0.055887   \n",
       "                          Equity(2 [AAP])       0.028611            0.051009   \n",
       "                          Equity(3 [AAPL])     -0.021972            0.035906   \n",
       "                          Equity(4 [ABBV])      0.006147            0.033018   \n",
       "\n",
       "                                            Tradable  \n",
       "2014-01-06 00:00:00+00:00 Equity(0 [A])        False  \n",
       "                          Equity(1 [AAL])      False  \n",
       "                          Equity(2 [AAP])      False  \n",
       "                          Equity(3 [AAPL])      True  \n",
       "                          Equity(4 [ABBV])     False  "
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Set starting and end dates\n",
    "start_date = pd.Timestamp('2014-01-06', tz='utc')\n",
    "end_date = pd.Timestamp('2016-01-05', tz='utc')\n",
    "\n",
    "# Run our pipeline for the given start and end dates\n",
    "output = engine.run_pipeline(pipeline, start_date, end_date)\n",
    "\n",
    "# Display the pipeline output\n",
    "output.head()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
